package com.cjj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjj.common.convention.exception.ServiceException;
import com.cjj.dao.entity.SecondhandOrder;
import com.cjj.dao.mapper.SecondhandOrderMapper;
import com.cjj.dto.req.CreateSecondhandProductReqDTO;
import com.cjj.dto.req.SearchProductReqDTO;
import com.cjj.dto.req.UpdateBySecondHandIdReqDTO;
import com.cjj.service.SecondhandOrderService;
import org.springframework.stereotype.Service;

import static com.cjj.common.convention.errorcode.BaseErrorCode.SERVICE_ERROR;

/**
 * <p>
 * 二手商品订单表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2025-10-25
 */
@Service
public class SecondhandOrderServiceImpl extends ServiceImpl<SecondhandOrderMapper, SecondhandOrder> implements SecondhandOrderService {

    /**
     * 创建二手物品订单
     */
    @Override
    public void createOrder(CreateSecondhandProductReqDTO createSecondhandProductReqDTO) {
        baseMapper.insert(BeanUtil.copyProperties(createSecondhandProductReqDTO,SecondhandOrder.class));
    }

    @Override
    public void updateByOrderId(UpdateBySecondHandIdReqDTO updateBySecondHandIdReqDTO) {
        LambdaQueryWrapper<SecondhandOrder> lambdaQuery = Wrappers.lambdaQuery(SecondhandOrder.class);
        lambdaQuery.eq(SecondhandOrder::getSecondHandId, updateBySecondHandIdReqDTO.getSecondHandId());
        SecondhandOrder secondhandOrder = baseMapper.selectOne(lambdaQuery);
        secondhandOrder.setPrice(updateBySecondHandIdReqDTO.getPrice());
        int count = baseMapper.updateById(secondhandOrder);
        if (count != 1){
            throw new ServiceException(SERVICE_ERROR);
        }
    }

    /**
     * 实现二手商品的分页查询
     */
    @Override
    public IPage<SecondhandOrder> searchProduct(Integer pageNum, Integer pageSize, SearchProductReqDTO searchProductReqDTO) {
        // 校验分页参数
        pageNum = (pageNum == null || pageNum < 1) ? 1 : pageNum;
        pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
        pageSize = Math.min(pageSize, 100); // 限制最大页大小

        // 创建分页对象
        Page<SecondhandOrder> page = new Page<>(pageNum, pageSize);
        if (searchProductReqDTO == null || StrUtil.isBlank(searchProductReqDTO.getProductName())) {
            LambdaQueryWrapper<SecondhandOrder> wrapper = Wrappers.lambdaQuery(SecondhandOrder.class);
            // 直接转换并返回 IPage
            return baseMapper.selectPage(page, wrapper);
        } else {
            // 构建查询条件
            LambdaQueryWrapper<SecondhandOrder> wrapper = Wrappers.lambdaQuery(SecondhandOrder.class)
                    .like(StrUtil.isNotBlank(searchProductReqDTO.getProductName()), SecondhandOrder::getProductName, searchProductReqDTO.getProductName());
            // 直接转换并返回 IPage
            return baseMapper.selectPage(page, wrapper);
        }
    }


}
